<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'bas:wms:pos:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBasWmsPosBas3dwhPosForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { treeData } from '/@/api/bas/wms/area/bas3dwhArea';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import {
    Bas3dwhPos,
    bas3dwhPosSave,
    bas3dwhPosForm,
    bas3dwhPosTreeData,
  } from '/@/api/bas/wms/pos/bas3dwhPos';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('bas.wms.pos.bas3dwhPos');
  const { showMessage } = useMessage();
  const record = ref<Bas3dwhPos>({} as Bas3dwhPos);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增缓存货位') : t('编辑缓存货位'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('上级缓存货位'),
      field: 'parentCode',
      fieldLabel: 'parentName',
      component: 'TreeSelect',
      componentProps: {
        allowClear: true,
        style: 'width: calc(50% - 60px)',
      },
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('货位编码'),
      field: 'code',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      required: true,
    },
    {
      label: t('货位名称'),
      field: 'name',
      component: 'Input',
      componentProps: {
        maxlength: 300,
      },
      required: true,
    },
    {
      label: t('所属区域'),
      field: 'areaCode',
      component: 'TreeSelect',
      componentProps: {
        api: treeData,
        immediate: true,
        // params: { isLoadUser: true, userIdPrefix: '' },
        // canSelectParent: false,
        allowClear: true,
      },
      required: true,
    },
    {
      label: t('行号'),
      field: 'irow',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ required: true }, { pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('列号'),
      field: 'icol',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ required: true }, { pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('当前箱子'),
      field: 'bxoCode',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('状态'),
      field: 'cstatus',
      component: 'Select',
      componentProps: {
        dictType: 'bas_wms_3dwh_pos_status',
        allowClear: true,
      },
    },
    {
      label: t('是否锁定'),
      field: 'block',
      component: 'Select',
      componentProps: {
        dictType: 'sys_yes_no',
        allowClear: true,
      },
    },
    {
      label: t('预计解锁时间'),
      field: 'unlockDate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
    },
    {
      label: t('锁定人'),
      field: 'loker',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('排序'),
      field: 'treeSort',
      helpMessage: '升序',
      component: 'InputNumber',
      defaultValue: '30',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ required: true }, { pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, updateSchema, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 12, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await bas3dwhPosForm(data);
    record.value = (res.bas3dwhPos || {}) as Bas3dwhPos;
    record.value.__t = new Date().getTime();
    if (data.parentCode && data.parentName) {
      record.value.parentCode = data.parentCode;
      record.value.parentName = data.parentName;
    }
    setFieldsValue(record.value);
    updateSchema([
      {
        field: 'parentCode',
        componentProps: {
          api: bas3dwhPosTreeData,
          params: {
            excludeCode: record.value.id,
            isShowRawName: true,
          },
        },
      },
      {
        field: 'code',
        componentProps: {
          disabled: !record.value.isNewRecord,
        },
      },
      {
        field: 'areaCode',
        componentProps: {
          disabled: !record.value.isNewRecord,
        },
      },
      {
        field: 'cstatus',
        componentProps: {
          disabled: !record.value.isNewRecord,
        },
      },
      {
        field: 'block',
        componentProps: {
          disabled: !record.value.isNewRecord,
        },
      },
      {
        field: 'unlockDate',
        componentProps: {
          disabled: !record.value.isNewRecord,
        },
      },
      {
        field: 'loker',
        componentProps: {
          disabled: !record.value.isNewRecord,
        },
      },
    ]);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        code: data.code,
      };
      const res = await bas3dwhPosSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
